$(document).on('click','.Startparsing',function (){
    let link=$('.link').val()
    if(!link){
        alert('请输入要解析的链接')
        return
    }
    $('.Startparsing').find('.loading').removeClass('divhidden')
    // 使用方法
    request('index.php','post',{link}).then(response => {
            $('.Startparsing').find('.loading').addClass('divhidden')

            if(response.code==1){
                if(response.data.imageAtlas.length){
                    $('.main-open-video').addClass('divhidden')
                    $('.main-open-imageAtlas').removeClass('divhidden')
                    $('.imageTitle').text(response.data.title)
                    $('.imageDescription').text(response.data.description)

                    let html=''
                    response.data.imageAtlas.forEach(item=>{
                        html+= `
<div class="flex-col">
     <div class="flex1 bg000 flex-row-center">
        <img class="width100p" src="${item}">
    </div>
    <div class="mt10 tac">
        <button type="button" class="btn btn-primary" onclick="copyText('${item}')">复制链接</button>
        <button type="button" class="btn btn-success" onclick="downloadFileWithProgress('${item}','图片.jpg')">下载图片</button>
    </div>
</div>
                        `
                    })
                    $('.imageAtlas').html(html)

                }else{
                    $('.main-open-imageAtlas').addClass('divhidden')
                    $('.main-open-video').removeClass('divhidden')

                    $('.shipinbiaoti').val(response.data.title)
                    $('.fengmiandizhi').val(response.data.imageSrc)
                    $('.shipindizhi').val(response.data.videoSrc)

                    $('.video').attr('poster',response.data.imageSrc)
                    $('.video').attr('src',response.data.videoSrc)
                    $('.videoTitle').text(response.data.title)
                    $('.videoDescription').text(response.data.description)
                }
            }else{
                alert(response.msg)
            }
        })
        .catch(error => {
            $('.Startparsing').find('.loading').addClass('divhidden')
            jqeruytoast('请求失败')
        });
})


/**
 * 复制
 */
$(document).on('click','.copyText',function (){
    let text=$(this).parents('.input-group').find('input').val()
    copyText(text)
})

/**
 * 下载图片
 */
$(document).on('click','.downImage',function (){
    let text=$(this).parents('.input-group').find('input').val()
    let imageExtension = getFileExtension(text)
    let len=imageExtension.indexOf('?')
    console.log(len)
    if( len>-1){
        imageExtension=imageExtension.slice(0,len)
    }
    downloadFileWithProgress(text,`图片.${imageExtension}`)
})

function getFileExtension(url) {
    // 查找URL中最后一个'.'的位置
    const lastDotIndex = url.lastIndexOf('.');

    // 检查是否找到了'.'并且它不是在URL的开头（例如'http://example.com'的情况）
    if (lastDotIndex !== -1 && lastDotIndex !== url.length - 1) {
        // 从最后一个'.'开始截取字符串直到URL的末尾
        return url.substring(lastDotIndex + 1);
    }

    // 如果没有找到有效的文件扩展名，返回null或空字符串
    return null; // 或者 return '';
}

/**
 * 下载视频
 */
$(document).on('click','.downVideo',function (){
    let text=$(this).parents('.input-group').find('input').val()
    downloadFileWithProgress(text,'视频.mp4')
})